package git.soulbgm.log;

import git.soulbgm.config.SysConfig;
import git.soulbgm.utils.JsonTool;
import git.soulbgm.utils.LogHelper;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 日志输出
 *
 * @author SoulBGM
 * @date 2024/03/01
 */
public class LogOutput {

    private static final LogHelper LOG = new LogHelper(LogOutput.class);

    private static SysConfig config;

    public static void setConfig(SysConfig config) {
        LogOutput.config = config;
    }

    private static final String KAFKA = "kafka";
    private static final Map<String, DebugInfo> DEBUG_INFO_MAP = new LinkedHashMap<>(3);

    static {
        DEBUG_INFO_MAP.put(KAFKA, new DebugInfo(KAFKA, "成功将数据放入Kafka输出日志", false));
    }

    public static Map<String, DebugInfo> getDebugInfoMap() {
        return DEBUG_INFO_MAP;
    }

    public static void setDebugMap(Map<String, Boolean> debugMap) {
        debugMap.forEach((k, v) -> {
            DebugInfo debugInfo = DEBUG_INFO_MAP.get(k);
            if (debugInfo != null) {
                debugInfo.setDebug(v);
            }
        });
    }

    /**
     * 设置方法的debug
     *
     * @param name debugInfo的名称
     */
    public static Boolean setDebug(String name) {
        DebugInfo debugInfo = DEBUG_INFO_MAP.get(name);
        Boolean debug = debugInfo.getDebug();
        debugInfo.setDebug(!debug);
        return debugInfo.getDebug();
    }

    /**
     * 成功将数据放入kafka输出日志
     *
     * @param topicName kafka主题名称
     * @param data      数据
     */
    public static void kafkaSuccessOutputLog(String topicName, Object data) {
        if (!config.isDebug()) {
            return;
        }
        if (!DEBUG_INFO_MAP.get(KAFKA).getDebug()) {
            return;
        }
        String json = "";
        String format = "Topic: {}, 存入Kafka成功";
        if (config.isDebugData()) {
            json = JsonTool.toJson(data);
            format += ", 数据: {}";
        }
        LOG.info(format, topicName, json);
    }

}
